package com.chehejia.devmng.platform.demo.demo1;

import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

import java.io.FileOutputStream;

/**
     * 描述:  单元格合并
 *
 * @author 王永坤
 * @createDate 2018-12-16 20:42
 */
@Slf4j
public class preAmountTemplate1 {
    public static void main(String[] args) {
        try {
            Integer weekSeq[] = {1,2,3,4,5,6,7,8,9,10,11,12,13};
            String vdcList[] = {"常州VDC","北京VDC","上海VDC","广州VDC"};
            // 创建 Excel 对象
            Workbook wb  = new HSSFWorkbook();
            // 创建 第一个sheet页
            Sheet sheet = wb.createSheet("sheet");
            // 创建第一个 row
            Row row = sheet.createRow(0);
            // 创建第一个 cell
            Cell cell = row.createCell(0);
            createCell(wb,row,"类别",(short)0,HSSFCellStyle.ALIGN_CENTER,HSSFCellStyle.VERTICAL_CENTER);
            createCell(wb,row,"VDC",(short)1,HSSFCellStyle.ALIGN_CENTER,HSSFCellStyle.VERTICAL_CENTER);
            for (Integer week : weekSeq) {
                log.info("周:{}",week);
                createCell(wb,row,"第"+week+"周",week+1,HSSFCellStyle.ALIGN_CENTER,HSSFCellStyle.VERTICAL_CENTER);
            }
            createRowAndCell(sheet,1,1,"产量");
            // CellRangeAddress 四个参数   起始行,结束行,其实列,结束列
            sheet.addMergedRegion(new CellRangeAddress(1,vdcList.length,0,0));
            // 初始值
            int startVDC = 1;
            // 设置VDC
            for (String vdc : vdcList) {
                log.info("VDC:{}",vdc);
                log.info("第几个VDC:{}",startVDC);
                createRowAndCell(sheet,1,1,"产量");
                createRowAndCell(sheet,startVDC,1,vdc);
                startVDC++;
            }
            FileOutputStream fs = new FileOutputStream("D:\\POI\\产量10.xls");
            wb.write(fs);
            fs.flush();
            fs.close();

        } catch (Exception e) {
            e.printStackTrace();
        }finally {

        }
    }

    /**
     *创建一个单元格, 为其设置 指定的格式
     * @param wb
     * @param row 行 传进的行
     * @param column 列数
     * @param halign  水平方向
     * @param valign 垂直方向
     */
    private static  void  createCell (Workbook wb,Row row,String cellValue,int column,short halign,short valign){
        Cell cell = row.createCell(column); // 创建单元格
        // 设置单元格的样式
        cell.setCellValue(cellValue);// 设置值
        CellStyle cellStyle =wb.createCellStyle();// 创建单元格样式
        cellStyle.setAlignment(halign); // 设置单元格 水平方向对齐方式
        cellStyle.setAlignment(valign);// 设置单元格 垂直方向 对齐方式
        cell.setCellStyle(cellStyle); //设置单元格样式

    }

    /**
     *
     * @param sheet sheet
     * @param RowNum 第几行
     * @param column 第几列
     * @param cellValue 列中的值
     *
     */
    private static  void  createRowAndCell (Sheet sheet,int RowNum,int column,String cellValue){
        Row sheetRow = sheet.createRow(RowNum);
        Cell rowCell = sheetRow.createCell(column);
        rowCell.setCellValue(cellValue);
    }

}
